<?php
namespace Database;

class Order extends Base
{

    public function __construct()
    {
        $this->setModel(MODEL_ORDER);
    }

    public function getList($userId, $statusIn, $page, $limit)
    {
        $sql = $this->getSql();
        $sql->field('*');
        $sql->table($this->getTable());
        $sql->where('dry_user', '=', $userId);
        $sql->andWhere('dry_status', 'in', $statusIn);
        $sql->setOrder('id', 'desc');
        $sql->setPage($page);
        $sql->setPageSize($limit);
        $sql->setLimit();
        $list = $this->fetchAll($sql->get());
        $count = $this->fetchColumn($sql->getTotalSql());
        return [
            'list' => $list,
            'count' => $count
        ];
    }

    public function getCount($userId)
    {
        $sql = $this->getSql();
        $sql->field('id');
        $sql->table($this->getTable());
        $sql->where('dry_user', '=', $userId);
        $sql->get();
        return $this->fetchColumn($sql->getTotalSql());
    }

    /*
        用户是否买过指定的产品(付款)
    */
    public function isBuy($userId, $productId)
    {
        $statusIn = [
            ORDER_STATUS_PAY,
            ORDER_STATUS_FINISH
        ];
        $statusIn = implode(',', $statusIn);
        $sql = $this->getSql();
        $sql->field('id');
        $sql->table($this->getTable());
        $sql->where('dry_user', '=', $userId);
        $sql->andWhere('dry_product_id', 'contain', $productId);
        $sql->andWhere('dry_status', 'in', $statusIn);
        $sql->get();
        return $this->fetchColumn($sql->getTotalSql());
    }

}